package com.shjn.squirrel.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.shjn.squirrel.R;
import com.shjn.squirrel.model.Constant;
import com.shjn.squirrel.model.UserInfoCache;
import com.shjn.squirrel.utils.SoundPlayUtils;
import com.shjn.squirrel.customView.BackAndTimerView;

/**
 * 用户投递结果未获取环保或积分的显示记录
 */
public class UserRecordNoneFragment extends BaseFragment implements View.OnClickListener {

    private static UserRecordNoneFragment instance;
    private TextView mTvTip01;
    private TextView mTvTip02;
    private Button mBtnOVer;
    private Button mBtnAgain;

    public UserRecordNoneFragment() {
    }

    public synchronized static UserRecordNoneFragment getInstance() {
        if (instance == null) {
            instance = new UserRecordNoneFragment();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return createView(inflater, R.layout.user_deliver_record_none, container);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initTimer();
        initView();
    }

    /**
     * 初始化倒计时器
     */
    private void initTimer() {
        //加载定时器面板，设置定时时间
        mActivity.mBackAndTimer.setOnBackListener(null).setBackVisibleState(false)
                .setOnTimerFinishListener(new BackAndTimerView.OnTimerFinishListener() {
                    @Override
                    public void onTimerFinish() {
                        //跳转到结束投递页
                        if (!UserFinishFragment.getInstance().isAdded()) {
                            mActivity.mBackAndTimer.stop();
                            SoundPlayUtils.play(16);//感谢使用提示音
                            mActivity.getFragmentManager().beginTransaction()
                                    .replace(R.id.mid, UserFinishFragment.getInstance())
                                    .commit();

                        }
                    }
                }).setTime(60).setVisibleState(true).start();
    }

    /**
     * 初始化相关控件
     */
    private void initView() {
        mTvTip01 = (TextView) mView.findViewById(R.id.tv_tip_01);
        mTvTip02 = (TextView) mView.findViewById(R.id.tv_tip_02);
        mBtnOVer = (Button) mView.findViewById(R.id.btn_over);
        mBtnAgain = (Button) mView.findViewById(R.id.btn_again);
        mBtnOVer.setOnClickListener(this);
        mBtnAgain.setOnClickListener(this);
        if (UserInfoCache.category.equals(Constant.glass) ||
                UserInfoCache.category.equals(Constant.harm)) {
            mTvTip01.setText("本次投递未获得环保积分");
            mTvTip02.setVisibility(View.INVISIBLE);
        } else {
            mTvTip01.setText("本次投递未获得环保金");
            mTvTip02.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //结束投递
            case R.id.btn_over:
                //跳转到结束投递页
                if (!UserFinishFragment.getInstance().isAdded()) {
                    mActivity.mBackAndTimer.stop();
                    SoundPlayUtils.play(16);//感谢使用提示音
                    mActivity.getFragmentManager().beginTransaction()
                            .replace(R.id.mid, UserFinishFragment.getInstance())
                            .commit();

                }
                break;
            //继续投递
            case R.id.btn_again:
                //回到首页
                if (!UserSelectFragment.getInstance().isAdded()) {
                    mActivity.mBackAndTimer.stop();
                    SoundPlayUtils.play(15);//选择垃圾类型提示音
                    mActivity.mBackAndTimer.setVisibleState(false);
                    mActivity.getFragmentManager().beginTransaction()
                            .replace(R.id.mid, UserSelectFragment.getInstance())
                            .commit();
                }
                break;
            default:
                break;
        }
    }
}
